import React,{Component} from 'react'
import LinearGradient from 'react-native-linear-gradient';
import {
    View,
    Text,
    Image,
    Dimensions,
    ToastAndroid,
    ImageBackground,
} from 'react-native';
import Config from '../config/config'
import pxToDp from '../config/pxToDp'
const {height, width} = Dimensions.get('window');
export default class LoginState extends Component{
    constructor(props){
        super(props)
        // 读取getAllDataForKey
        // storage.getAllDataForKey('userInfo').then(ids => {
        //     console.log(ids,123);
        // });
       
        storage.load({
                key: 'loginState',
        }).then(ret => {
            global.uid = ret.userId
            // 如果找到数据，则在then方法中返回
            fetch(Config.Url + "?m=User&c=Api&a=is_login",{
                method:'POST',
                headers:{
                    'Content-Type': 'application/x-www-form-urlencoded'
                },
                body: "uid=" + ret.userId + "&token=" + ret.token
            }).then(response =>{
                response.json().then(content=>{                 
                    if (content.state.code == '0'){
                        
                        storage.save({
                            key: 'loginState', // 注意:请不要在key中使用_下划线符号!
                            data: {
                                userId: content.data.uid,
                                token: content.data.token,
                            },
                        });
                        storage.save({
                            key: 'userInfo', // 注意:请不要在key中使用_下划线符号!
                            id: 1,
                            data: {
                                phone: content.data.phone,
                                nickname: content.data.nickname,
                                headsmall: content.data.headsmall == '' ? '../imgs/pic.jpg' : content.data.headsmall,
                            },
                        });
                       
                        this.props.navigation.replace('homeScene', {
                            name: 'homeScene'
                        })
                    }else{
                        this.props.navigation.replace('accountLogin', {
                            name: 'accountLogin'
                        })
                    }
                })
            })
            
        }).catch((err)=>{
            this.props.navigation.replace('accountLogin', {
                name: 'accountLogin'
            })
        })
        
    }
    render(){
        return(
            <View style={styles.view} >
                <LinearGradient colors={['rgb(91,212,227)', 'rgb(50,180,254)']} style={{flex:1}}>
                    <View style={styles.add}>
                        <Image style={styles.logo}  source={require('../imgs/ads2.png')} />
                        <Text style={styles.text}>
                            随聊
                        </Text>
                    </View>
                    
                </LinearGradient>
            </View>
        )
    }
}

const styles = {
    view:{
        flex:1,
    },
    add:{
        flex: 1,
        alignItems:'center',
        justifyContent:'space-between',
        paddingBottom:20,
    },
    logo:{
        width:130,
        height:120,
        marginTop:100,
        alignSelf:'center',
    },
    text:{
        fontSize:22,
        color:'#fff',
    },
}